package com.example.springboot2.oa.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot2.base.web.BaseController;
import com.example.springboot2.oa.entity.TUser;
import com.example.springboot2.tools.MakeResponse;
import com.example.springboot2.tools.ResponseMessage;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.function.Consumer;

@CrossOrigin
@RestController
@RequestMapping("/oa/t-user")
public class TUserController extends BaseController {
    /**
     * 登陆验证
     */
    @PostMapping("login")
    public ResponseMessage login(@RequestBody TUser user, HttpSession session) {
        QueryWrapper<TUser> wrapper = new QueryWrapper<TUser>();
        wrapper.eq("user_name", user.getUserName())
                .eq("user_pwd", user.getUserPwd());

        TUser tUser = userService.getOne(wrapper);
        if (tUser == null) {
            return MakeResponse.success("500", "用户名不存在");
        }

        String token = String.valueOf(UUID.randomUUID());
        tUser.setToken(token);
        QueryWrapper<TUser> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("id", tUser.getId());

        if (!userService.update(tUser, wrapper1)) {
            return MakeResponse.success("500", "登陆失败");
        }

        return MakeResponse.success("200", "登陆成功", tUser);
    }

    /**
     * 退出登陆
     * @param userId
     * @param request
     * @return
     */
    @GetMapping("logout")
    public ResponseMessage logout(String userId, HttpServletRequest request) {
        TUser user = new TUser();
        user.setId(userId);
        user.setToken("");

        QueryWrapper<TUser> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("id", user.getId());
        userService.update(user, wrapper1);
        return MakeResponse.success("200", "退出成功");
    }

    /**
     * 更新密码
     */
    @PostMapping("setPass")
    public ResponseMessage setPass(String userId, String userPwd, HttpServletRequest request) {
        QueryWrapper<TUser> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("token", request.getHeader("token"));
        if (userService.getOne(wrapper1) == null) {
            return MakeResponse.success("401", "无权访问");
        }

        TUser user = new TUser();
        user.setId(userId);
        user.setUserPwd(userPwd);
        if (!userService.updateById(user)) {
            return MakeResponse.success("500", "密码设置失败");
        }

        return MakeResponse.success("200", "密码设置成功");
    }

    /**
     * 用户列表
     * @param request
     * @return
     */
    @GetMapping("listTeacher")
    public ResponseMessage listTeacher(HttpServletRequest request) {
        ResponseMessage rsp = authentication(request);
        if (rsp != null) {
            return rsp;
        }

        QueryWrapper<TUser> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", "1");
        List<TUser> data = userService.list(wrapper);
        return MakeResponse.success("200", "成功", data);
    }

    /**
     * 用户列表
     * @param request
     * @return
     */
    @PostMapping("listAll")
    public ResponseMessage listAll(HttpServletRequest request) {
        ResponseMessage rsp = authentication(request);
        if (rsp != null) {
            return rsp;
        }

        QueryWrapper<TUser> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("create_time");
        List<TUser> data = userService.list(wrapper);
        return MakeResponse.success("200", "成功", data);
    }

    /**
     * 添加用户
     */
    @PostMapping("add")
    public ResponseMessage add(@RequestBody TUser user, HttpServletRequest request) {
        ResponseMessage rsp = authentication(request);
        if (rsp != null) {
            return rsp;
        }

        QueryWrapper<TUser> userNameWrapper = new QueryWrapper<>();
        userNameWrapper.eq("user_name", user.getUserName());
        TUser userFlag = userService.getOne(userNameWrapper);
        if (userFlag != null) {
            return MakeResponse.success("500", "用户名已经存在");
        }

        user.setId(String.valueOf(UUID.randomUUID()));
        user.setCreateTime(new Date());
        user.setUserPwd("000000");
        user.setUserState(0);

        if (!userService.save(user)) {
            return MakeResponse.success("500", "新增失败");
        }

        return MakeResponse.success("200", "新增成功");
    }

    /**
     * 批量删除用户
     * @param userList
     * @param request
     * @return
     */
    @PostMapping("delete")
    public ResponseMessage delete(@RequestBody List<TUser> userList, HttpServletRequest request) {
        ResponseMessage rsp = authentication(request);
        if (rsp != null) {
            return rsp;
        }

        List<String> userIdList = new ArrayList<>();
        userList.forEach(new Consumer<TUser>() {
            @Override
            public void accept(TUser user) {
                userIdList.add(user.getId());
            }
        });

        if (!userService.removeByIds(userIdList)) {
            return MakeResponse.success("500", "批量删除失败");
        }

        return MakeResponse.success("200", "批量删除成功");
    }

    /**
     * 更新用户
     * @param user
     * @param request
     * @return
     */
    @PostMapping("modify")
    public ResponseMessage modify(@RequestBody TUser user, HttpServletRequest request) {
        ResponseMessage rsp = authentication(request);
        if (rsp != null) {
            return rsp;
        }

        QueryWrapper<TUser> userNameWrapper = new QueryWrapper<>();
        userNameWrapper.eq("user_name", user.getUserName())
                .ne("id", user.getId());
        TUser user2 = userService.getOne(userNameWrapper);
        if (user2 != null){
            return MakeResponse.success("500", "用户名已经存在");
        }

        user.setUpdateTime(new Date());
        if (!userService.updateById(user)) {
            return MakeResponse.success("500", "修改失败");
        }

        return MakeResponse.success("200", "修改成功");
    }

    /**
     * 更新状态
     * @param user
     * @param request
     * @return
     */
    @PostMapping("state")
    public ResponseMessage state(@RequestBody TUser user, HttpServletRequest request) {
        ResponseMessage rsp = authentication(request);
        if (rsp != null) {
            return rsp;
        }

        QueryWrapper<TUser> wrapper2 = new QueryWrapper();
        wrapper2.eq("id", user.getId());
        user = userService.getOne(wrapper2);
        if (user == null) {
            return MakeResponse.success("500", "用户不存在");
        }

        if (user.getUserState() == 0) {
            user.setUserState(1);
        } else {
            user.setUserState(0);
        }

        if (!userService.updateById(user)) {
            return MakeResponse.success("500", "设置状态失败");
        }

        return MakeResponse.success("200", "设置状态成功");
    }

    /**
     * 重置密码
     * @param user
     * @param request
     * @return
     */
    @PostMapping("resetPwd")
    public ResponseMessage resetPwd(@RequestBody TUser user, HttpServletRequest request) {
        ResponseMessage rsp = authentication(request);
        if (rsp != null) {
            return rsp;
        }

        QueryWrapper<TUser> wrapper = new QueryWrapper();
        wrapper.eq("id", user.getId());
        user = userService.getOne(wrapper);
        if (user == null) {
            return MakeResponse.success("500", "用户不存在");
        }

        user.setUserPwd("000000");
        if (!userService.updateById(user)) {
            return MakeResponse.success("500", "重置密码失败");
        }

        return MakeResponse.success("200", "重置密码成功 000000");
    }

    /**
     * 留言列表
     * @return
     */
    @ApiOperation(value = "留言列表")
    @GetMapping("listUserContent")
    public ResponseMessage listUserContent(String userName,HttpServletRequest request){
        //验证是否登陆
        QueryWrapper<TUser> userToken = new QueryWrapper<>();
        userToken.eq("token",request.getHeader("token"));
        TUser tUser = userService.getOne(userToken);
        if (tUser == null){
            return MakeResponse.success("401","无权限访问",null);
        }
        QueryWrapper<TUser> wrapper = new QueryWrapper<>();
        wrapper.ne("id",tUser.getId()).eq("user_state",1).like("user_name",userName);
        List<TUser> data = userService.list(wrapper);
        return MakeResponse.success("200","用户列表",data);
    }
}